//c++11 提供的正则表达式库操作std::string对象，模式std::regex，通过std::regex_match进行匹配,
//从而产生std::smatch结果
//regex_match匹配成功返回true 否则返回false
#include <string>
#include <regex>
#include "include.h"

void test1()
{
	std::string fnames[]={"foo.txt","bar.txt","test","a0.txt","AAA.txt"};
	std::regex txt_regex("[a-z]+\\.txt");
	for(const auto &fname:fnames)
	{
		cout<<fname<<": "<<std::regex_match(fname,txt_regex)<<endl;
	}
	//输出结果
	/*
		foo.txt: 1
		bar.txt: 1
		test: 0
		a0.txt: 0
		AAA.txt: 0
	*/
}
void test2()
{
	std::string fnames[]={"foo.txt","bar.txt","test","a0.txt","AAA.txt"};
	std::regex txt_regex("([a-z]+)\\.txt");
	std::smatch base_match;//结果
	for(const auto &fname:fnames)
	{
		if(std::regex_match(fname,base_match,txt_regex))
		{
			if(base_match.size()==2)
			{
				//0 匹配整个字符串
				//1 匹配第一个括号表达式
				cout<<base_match[0]<<" "<<base_match[1]<<endl;
			}
		}
	}
	/*输出结果
		foo.txt foo
		bar.txt bar
	*/
}
void test_regex_match()
{
	test1();
	test2();
}